package com.kylin.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kylin.common.core.constant.Constants;
import com.kylin.common.core.service.impl.DataServiceImpl;
import com.kylin.common.redis.service.RedisService;
import com.kylin.system.common.enums.SystemRedisKeys;
import com.kylin.system.entity.SysConfig;
import com.kylin.system.mapper.SysConfigMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 系统参数 服务层处理
 *
 * @author wuhao
 * @version 1.0 - 2021-03-28
 */
@Service
public class SysConfigService extends DataServiceImpl<SysConfigMapper, SysConfig> {

    @Autowired
    private RedisService redisService;

    /**
     * 项目启动时，初始化参数到缓存
     */
//    @PostConstruct
//    public void init()
//    {
//        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
//        List<SysConfig> configsList = this.mapper.selectList(queryWrapper);
//        for (SysConfig config : configsList)
//        {
//            redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
//        }
//    }

    /**
     * 根据键名查询参数配置信息
     * @param configKey 配置key
     * @return  参数键值
     * @version 1.0  by wuhao at 2021-03-28
     */
    public String selectConfigByKey(String configKey)
    {
        String configValue = redisService.getCacheObject(getCacheKey(configKey));
        if (StringUtils.isNotEmpty(configValue))
        {
            return configValue;
        }
        QueryWrapper<SysConfig> where = new QueryWrapper<>();
        where.eq("config_key", configKey);
        SysConfig retConfig = this.mapper.selectOne(where);
        if (ObjectUtils.isNotEmpty(retConfig))
        {
            redisService.setCacheObject(getCacheKey(configKey), retConfig.getConfigValue());
            return retConfig.getConfigValue();
        }
        return StringUtils.EMPTY;
    }


    /**
     * 插入参数配置
     * @param sysConfig 参数配置对象
     * @return 新增数量
     * @version 1.0  by wuhao at 2021-05-01
     */
    @Override
    public int insert(SysConfig sysConfig) {
        int count = super.insert(sysConfig);
        if(count > 0) {
            redisService.setCacheObject(getCacheKey(sysConfig.getConfigKey()), sysConfig.getConfigValue());
        }
        return count;
    }

    /**
     * 更新参数配置
     * @param sysConfig 参数配置对象
     * @return 影响行数
     * @version 1.0  by wuhao at 2021-05-01
     */
    @Override
    public int update(SysConfig sysConfig) {
        int count = super.update(sysConfig);
        if(count > 0) {
            redisService.setCacheObject(getCacheKey(sysConfig.getConfigKey()), sysConfig.getConfigValue());
        }
        return count;
    }

    /**
     * 批量删除参数
     * @param ids 删除数据id列表
     * @return 删除数量
     * @version 1.0  by wuhao at 2021-05-01
     */
    @Override
    public int deleteBatchIds(List<Serializable> ids) {
        int count = super.deleteBatchIds(ids);
        if (count > 0)
        {
            Collection<String> keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*");
            keys.forEach(key -> redisService.deleteObject(key));
//            redisService.deleteObject(keys);
        }
        return count;
    }

    /**
     * 清空缓存数据
     */
    @Override
    public void clearCache()
    {
        Collection<String> keys = redisService.keys(Constants.SYS_CONFIG_KEY + "*");
        keys.forEach(key -> redisService.deleteObject(key));
//        redisService.deleteObject(keys);
        redisService.deleteObject(SystemRedisKeys.SYS_CONFIG_CUSTOM_KEY.key());
    }


    /**
     * 根据键名查询参数配置信息
     * @param group 配置key组
     * @return  参数键值
     * @version 1.0  by wuhao at 2021-03-28
     */
    public List<SysConfig> selectConfigByGroup(String group)
    {
        String key = Constants.SYS_CONFIG_GROUP_KEY + group;
        List<SysConfig> configList = redisService.getCacheList(key);
        if (ObjectUtils.isNotEmpty(configList))
        {
            return configList;
        }

        QueryWrapper<SysConfig> where = new QueryWrapper<>();
        where.eq("config_group", group);
        List<SysConfig> retConfigList = this.mapper.selectList(where);
        if (ObjectUtils.isNotEmpty(retConfigList))
        {
            redisService.setCacheList(key, retConfigList);
            return retConfigList;
        }
        return null;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey)
    {
        return Constants.SYS_CONFIG_KEY + configKey;
    }

    /**
     * 获取自定义配置列表
     * @return
     * @author wuhao
     * @version 1.0.0 - 2024/3/4
     */
    public List<SysConfig> getCustomConfigList() {
        List<SysConfig> customConfigList = redisService.getCacheList(SystemRedisKeys.SYS_CONFIG_CUSTOM_KEY.key());
        if(customConfigList == null || customConfigList.isEmpty()){
            customConfigList = this.getBaseMapper().selectList(new QueryWrapper<SysConfig>().eq("config_type", "N").eq("status", "0"));
            redisService.setCacheList(SystemRedisKeys.SYS_CONFIG_CUSTOM_KEY.key(), customConfigList);
        }
        return customConfigList;
    }
}
